<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>on</title>
</head>

<body>
    <div id="content"></div>
    <button class="ew-btn" type="button" id="addEventBtn">点击我添加事件</button>
    <button class="ew-btn" type="button" id="removeEventBtn">点击我移除事件</button>
    <script src="https://www.eveningwater.com/static/plugin/log-util.js"></script>
    <script type="text/javascript">
        function convertImageToBase64(image) {
            const canvas = document.createElement('canvas');
            const context = canvas.getContext('2d');
            canvas.width = image.width;
            canvas.height = image.height;
            context.drawImage(image, 0, 0);
            return canvas.toDataURL('image/png'); // 根据需要指定输出格式
        }

        const showImageInConsole = src => {
            const img = new Image();
            img.setAttribute('src', src);
            document.body.appendChild(img);
            img.onload = () => {
                const base64 = convertImageToBase64(img);
                const css = [
                    "padding: " + (img.height / 2 - 8) + "px " + img.width / 2 + "px;",
                    "line-height: " + img.height + "px;",
                    "background: url(" + base64 + ")no-repeat center / cover;",
                ];
                console.log("%c ", css.join(""));
                consoleUtil(base64);
                document.body.removeChild(img);
            }
        }
        const off = (el, type, callback, useCapture = false) => {
            if (el && typeof type === 'string' && typeof callback === 'function') {
                el.removeEventListener(type, callback, useCapture);
            }
        }
        const on = (el, type, handler, opts = {}) => {
            const delegatorFn = e => e.target.matches(opts.target) && handler.call(e.target, e);
            if (el && type && typeof handler === 'function') {
                el.addEventListener(type, opts.target ? delegatorFn : handler, opts.options || false);
                if (opts.target) {
                    return delegatorFn;
                }
            }
        }
        const callback = (...args) => {
            console.log(args);
            document.getElementById('content').textContent = '你看这不就是惊喜吗?打开浏览器控制台会有更多惊喜哦!';
            showImageInConsole('./1.png');
        };
        on(document.getElementById('addEventBtn'), 'click', callback);
        const offCallback = () => {
            off(document.getElementById('addEventBtn'), 'click', callback);
        }
        // 添加事件
        on(document.getElementById('removeEventBtn'), 'click', offCallback);
    </script>
</body>

</html>